package com.songtech.uitest.module.propose;

import com.songtech.uitest.components.IBrowserDriverFactory;
import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.LogInAndOutHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.util.AutoWait;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class PickupMobileID {

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    @Qualifier("browserDriver")
    private IBrowserDriverFactory driverCreater;

    @Autowired
    private LogInAndOutHandler logInAndOutHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(PickupMobileID.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    /**
     *
     * @param step 两位，如00，十位代表是否通过，个位代表谁登录。0为主账号主浏览器登录，>0的为辅账号辅浏览器登录
     * @return
     * @throws Exception
     */
    public String pickupMobileID(String step) throws Exception{

        WebDriver driver = stepParser.parseDriver(step);
        AutoWait.setWebDriverWait(driver);



        //关闭之前所有打开的页面，以免混乱
        tabHandler.closeAllTabs(driver);

        //回到需求提出页获取移动侧订单号
        menuHandler.clickMenuItem(driver, "铁塔建设", "需求提出", "");

        //到页面上抓取移动侧订单号
        AutoWait.jumpIntoFrameCheckLoading(By.name("iframeundefined"), 0);

        WebElement mIdLink = driver.findElement(By.xpath("//tbody/tr[1]/td[6]/a"));

        indent.setMobileId(mIdLink.getText());


        logger.info(space.getSpace() + "获得移动侧单号：" + indent.getMobileId());

        //关闭此页
        tabHandler.closeAllTabs(driver);

        indent.setStepPosition(indent.getStepPosition() + 1);

        return indent.getMobileId();




    }

}
